package forum_system_test;

import org.junit.jupiter.api.*;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

/**
 * @author tongchen
 * @create 2023-08-21 15:50
 */
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class PersonalCenterTest {
    //创建ChromeDriver
    private static ChromeDriver chromeDriver=TestUtils.getDriver();

    @BeforeAll
    public  static void loginAndClick() throws InterruptedException {
        chromeDriver.get("http://47.93.222.41:58080/sign-in.html");
        WebElement element = chromeDriver.findElement(By.cssSelector("#username"));
        WebElement element1 = chromeDriver.findElement(By.cssSelector("#password"));
        WebElement element2 = chromeDriver.findElement(By.cssSelector("#submit"));
        element.clear();
        element1.clear();
        element.sendKeys("刘健磊");
        element1.sendKeys("123456");
        //强制等待一秒，使新页面能加载出来
        element2.click();
        Thread.sleep(5000);
        //寻找具体元素并打开
        chromeDriver.findElement(By.cssSelector("#index_nav_avatar")).click();
        chromeDriver.findElement(By.cssSelector("#index_user_settings")).click();
    }
    @Test
    @Order(1)
    public void testExists() throws InterruptedException {
        //强制休眠，使元素能够完全加载
        Thread.sleep(3000);
        chromeDriver.findElement(By.cssSelector("#settings_avatar"));
        chromeDriver.findElement(By.cssSelector("#bit-forum-content > div.page-body > div > div > div > div.col.d-flex.flex-column > div > div.row.align-items-center > div:nth-child(2) > a"));
        chromeDriver.findElement(By.cssSelector("#setting_input_nickname"));
        chromeDriver.findElement(By.cssSelector("#setting_input_email"));
        chromeDriver.findElement(By.cssSelector("#setting_input_phoneNum"));
        chromeDriver.findElement(By.cssSelector("#settings_input_oldPassword"));
        chromeDriver.findElement(By.cssSelector("#settings_input_newPassword"));
        chromeDriver.findElement(By.cssSelector("#settings_input_passwordRepeat"));
        chromeDriver.findElement(By.cssSelector("#setting_submit_nickname"));
        chromeDriver.findElement(By.cssSelector("#setting_submit_email"));
        chromeDriver.findElement(By.cssSelector("#setting_submit_phoneNum"));
        chromeDriver.findElement(By.cssSelector("#settings_submit_password"));
        chromeDriver.findElement(By.cssSelector("#settings_textarea_remark"));
    }
    @Test
    @Order(2)
    public void testSubmit() throws InterruptedException {
        //找到元素并进行点击操作
        //强制休眠
        Thread.sleep(3000);
        WebElement nameInput = chromeDriver.findElement(By.cssSelector("#setting_input_nickname"));
        nameInput.clear();
        nameInput.sendKeys("陈桐1");
        //点击修改按钮
        chromeDriver.findElement(By.cssSelector("#setting_submit_nickname。。")).click();
    }
    @Order(3)
    @Test
    public void modifyPassword() throws InterruptedException {
        Thread.sleep(3000);
        //找到元素并进行正确输入
//        chromeDriver.findElement(By.cssSelector("#settings_input_oldPassword")).sendKeys("123456");
//        chromeDriver.findElement(By.cssSelector("#settings_input_newPassword")).sendKeys("123456");
//        chromeDriver.findElement(By.cssSelector("#settings_input_passwordRepeat")).sendKeys("123456");
        //点击按钮
       // chromeDriver.findElement(By.cssSelector("#settings_submit_password")).click();
        //使原密码和新密码一致
        //使新密码和确认密码不一致
//        chromeDriver.findElement(By.cssSelector("#settings_input_oldPassword")).sendKeys("123456");
//        chromeDriver.findElement(By.cssSelector("#settings_input_newPassword")).sendKeys("123456");
//        chromeDriver.findElement(By.cssSelector("#settings_input_passwordRepeat")).sendKeys("654321");
        //点击按钮
        //等待
        chromeDriver.findElement(By.cssSelector("#settings_input_oldPassword")).sendKeys("123456");
        chromeDriver.findElement(By.cssSelector("#settings_input_newPassword")).sendKeys("654321");
        chromeDriver.findElement(By.cssSelector("#settings_input_passwordRepeat")).sendKeys("654321");
        //Thread.sleep(5000);
        //元素滚动
        WebElement webElement = chromeDriver.findElement(By.cssSelector("#settings_submit_password"));//找到这个元素
        ((JavascriptExecutor) chromeDriver).executeScript("window.scrollTo("+webElement.getLocation().getX()+","+webElement.getLocation().getY()+")");
        Thread.sleep(7000);
        chromeDriver.findElement(By.cssSelector("#settings_submit_password")).click();
    }
}
